/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.filehandling.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import softstarter.launching.RunnableItem;


public class XMLDocumentReader {

	public List<RunnableItem> parseFileObjects(String fileName) {
	    
		List<RunnableItem> runnableItems = new LinkedList<RunnableItem>();
		
		try {
			runnableItems = parseDocument(fileName);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return runnableItems;
	}
	
	private List<RunnableItem> parseDocument(String fileName) throws Exception {
		
		List<RunnableItem> runnableItems = null;
		
//		XMLReader xr = XMLReaderFactory.createXMLReader();
//		FileObjectDocumentHandler handler = new FileObjectDocumentHandler();
//		xr.setContentHandler(handler);
//		xr.setErrorHandler(handler);
//
//		xr.parse(new InputSource(new FileReader(fileName)));
//		
//		runnableItems = handler.getFileObjects();
		
		SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
		
		FileObjectDocumentHandler handler = new FileObjectDocumentHandler();
	    InputStream inputStream= new FileInputStream(new File(fileName));
	    Reader reader = new InputStreamReader(inputStream,"UTF-8");

	    InputSource is = new InputSource(reader);
	    is.setEncoding("UTF-8");

	    saxParser.parse(is, handler);
		
	    runnableItems = handler.getFileObjects();
	    System.out.println("read objects: " + handler.getFileObjects());
		
	    if(runnableItems == null)
			return new LinkedList<RunnableItem>();
		else
			return runnableItems;
	}
	
//	private List<RunnableItem> getTopItemsFromFile(int numberOfHits, String fileName) throws Exception {
//	       List<RunnableItem> runnableItems;
//	        
//	        XMLReader xr = XMLReaderFactory.createXMLReader();
//	        FileObjectDocumentHandler handler = new FileObjectDocumentHandler();
//	        xr.setContentHandler(handler);
//	        xr.setErrorHandler(handler);
//
//	        xr.parse(new InputSource(new FileReader(fileName)));
//	        
//	        runnableItems = handler.getFileObjects();
//	        
//	        if(runnableItems == null)
//	            return new LinkedList<RunnableItem>();
//	        else
//	            return runnableItems;
//
//	}
	
	public static void main(String[] args) {
//		XMLDocumentReader x = new XMLDocumentReader();
//		
//		LinkedList<RunnableItem> objs = (LinkedList)x.parseFileObjects("fileobjects.xml");

	}
}
